package com.zy.mc_module.utils;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.fn.sdk.api.reward.FnRewardAd;
import com.fn.sdk.api.reward.FnRewardAdListener;
import com.zy.mc_module.common.Config;

public class AdRewardUtil {
    private boolean fnLoadOnly = false;
    private String userId;
    private String extraInfo;

    public static AdRewardUtil getInstance() {
        return new AdRewardUtil();
    }

    public void start(@NonNull Activity activity, @NonNull String userId, String extraInfo) {
        this.userId = userId;
        this.extraInfo = extraInfo;
        if (fnLoadOnly == true) {
            //加载成功之后播放
            FnRewardAd.getInstance().showOnly();
        } else {
            Log.e("xxxxx", "加载中... ");
            //第一次拉取广告，或加载广告失败的情况，走loadAd正常流程
            loadAd(activity);
        }
    }

    private void loadOnly(Activity activity) {
        FnRewardAd rewardAd = FnRewardAd.getInstance();
        rewardAd.setUserId(userId);
        if (extraInfo != null)
            rewardAd.setExtraInfo(extraInfo);
        rewardAd.loadOnly(activity, Config.reWardId, new FnRewardAdListener() {
            @Override
            public void onLoaded() {
                fnLoadOnly = true;
//                if(rewardAd!=null)rewardAd.show();
                Log.d("xxxxxxxx预加载成功1111", "onLoaded: ");
                Toast.makeText(activity, "onLoaded", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCached() {

            }

            @Override
            public void onShow() {
                Toast.makeText(activity, "onShow", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onExpose() {
                Toast.makeText(activity, "onExpose", Toast.LENGTH_SHORT).show();
                Log.d("xxxxxxxx执行下次预加载11111", "onLoaded: ");

                //广告曝光回调
                //**********执行下次预加载*******
                loadOnly(activity);
            }

            @Override
            public void onReward(String orderId) {
                Log.e("xxx", "onReward:" + orderId);
                Toast.makeText(activity, "onReward:" + orderId, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClick() {
                Toast.makeText(activity, "onClick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {
                Toast.makeText(activity, "onComplete", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClose() {
                Toast.makeText(activity, "onClose", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(int code, String message, String detail) {
                String msg = String.format("error [%d - %s]", code, message, detail);
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
                Log.e(activity.getLocalClassName(), msg + "---detail:" + detail);
                //加载广告失败的情况，下次点击广告按钮时，走loadAd正常流程
                fnLoadOnly = false;
            }
        });
    }

    private void loadAd(Activity activity) {
        FnRewardAd rewardAd = new FnRewardAd();
        rewardAd.setUserId(userId);
        if (extraInfo != null)
            rewardAd.setExtraInfo(extraInfo);
        rewardAd.loadAd(activity, Config.reWardId, new FnRewardAdListener() {
            @Override
            public void onError(int code, String s, String detail) {
                String msg = String.format("error [%d - %s]", code, s, detail);
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
                Log.e(activity.getLocalClassName(), msg + "- detail:" + detail);
            }

            @Override
            public void onLoaded() {
                Log.d("xxxxxxxx正常流程加载成功1111", "onLoaded: ");
                Toast.makeText(activity, "onLoaded", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCached() {
                Toast.makeText(activity, "onCached", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShow() {
                Toast.makeText(activity, "onShow", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onExpose() {
                Toast.makeText(activity, "onExpose", Toast.LENGTH_SHORT).show();

                //广告曝光回调
                //执行下次预加载
                loadOnly(activity);
            }

            @Override
            public void onReward(String orderId) {
                Log.d("orderidxxxxxx", "onReward: " + orderId);
                Toast.makeText(activity, "onReward:" + orderId, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClick() {
                Toast.makeText(activity, "onClick", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {
                Toast.makeText(activity, "onComplete", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClose() {
                Log.d("xxxxxx", "onClose: ");
                Toast.makeText(activity, "onClose", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
